﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace SharpSphinx.Util
{
   public  class Timer
    {

       private String name; 


       private double sum;
       private long count; 
       private long startTime; 
       private long curTime;
       private long minTime = long.MaxValue; 
       private long maxTime; 
       private bool notReliable;


       Timer(string name)
       {

           Debug.Assert(name != null);  
           this.name = name; 
           

       }



       public String Name
       {
           get
           {
               return name;
           }

       }


       public void Reset()
       {
           startTime = 0L;
           count = 0L;
           sum = 0L;
           minTime = long.MaxValue;
           maxTime = 0L;
           notReliable = false; 

       }

       public bool IsStarted()
       {
           return (startTime > 0L);
       }

       public void Start()
       {
           if (startTime != 0L)
           {
               notReliable = true;
               Debug.WriteLine(Name + "Timer.Start() called without a Stop()"); 
           }
           startTime = DateTime.Now.Ticks; 

       }

       public void Start(long time)
       {
           if (startTime != 0L)
           {
               notReliable = true; 

           }

           if (time > DateTime.Now.Ticks)
           {
               throw new Exception("Start time is later than current time");

           }

           startTime = time;
       }

       public long Stop(bool verbose)
       {

           if (startTime == 0L)
           {

               notReliable = true; 

           }

           curTime = DateTime.Now.Ticks - startTime;
           startTime = 0L;
           if (curTime > maxTime)
           {
               maxTime = curTime;
           }

           if (curTime < minTime)
           {
               minTime = curTime;
           }

           count++;
           sum += curTime;
           if (verbose)
           {
               dump();
           }

           return curTime;

       }

       public void Stop()
       {
           Stop(false);
       }

       public void dump()
       {
           ShowTimesShort();
       }

       public long Count
       {
           get
           {
               return count;
           }
       }

       public long CurTime
       {
           get
           {
               return curTime;
           }
       }

       public double AverageTime
       {
           get
           {
               if (count == 0)
                   return 0.0;

               return sum / count;
           }
       }

       public long MinTime
       {
           get
           {
               return minTime; 
           }
       }

       public long MaxTime
       {
           get
           {
               return maxTime;
           }
       }

       private string fmtTime(long time)
       {
           return fmtTime(time / 1000.0);


       }

       private string fmtTime(double time)
       {
         //  return Utilities.Pad(timeFormatter.Format(time))
       }

       private void ShowTimeShort()
       {

           double avgTimes = 0.0;

           if (count == 0)
           {
               return;
           }


           if (count > 0)
           {
               avgTimes = sum / count / 1000.0;

               if (notReliable)
               {
                   //  Debug.WriteLine()
               }
               else
               {




               }
           }
       }
    }
}
